package com.parkreport.report.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.xml.rpc.ServiceException;

import com.common.jdbc.DB4ObjUtil;
import com.common.report.service.ReportService;
import com.common.report.vo.ReportCountDataVO;
import com.common.report.vo.ReportDataVO;
import com.common.report.vo.ReportTitleVO;
import com.common.util.CalendarUtil;
import com.common.util.StringUtil;
import com.parkreport.vo.ReportParamVO;

public class AreaCarNextReportService extends ReportService {

	private String titles;
	private String ments;
	List<ReportCountDataVO> subList = new ArrayList<ReportCountDataVO>();
	private String type = "areaCarNext";
	@Override
	public List findDatas(HttpServletRequest req) throws ServiceException {
		
		
		List<ReportCountDataVO> list = new ArrayList<ReportCountDataVO>();
		
		if("auto".equals(dateType)){
				
				list = findDatas(startDate,endDate);
			}else if("day".equals(dateType)){
				
				list =findDatesByType(type,date);
			}else if("month".equals(dateType)){
				
				list =findDatesByType(type,date);
			}else if("year".equals(dateType)){
				
				list =findDatesByType(type,date);
			}
			
			return list;
	}
	
	public void excuteTimer(String dateType){
		CalendarUtil calUtil = new CalendarUtil();
		date = "";
		if("day".equals(dateType)){
			date = calUtil.getPreviousDay();
			startDate  = date;
			endDate = date;
		}else if("month".equals(dateType)){
			date = calUtil.getPreviousMonth();
			 calUtil = new CalendarUtil();
			startDate  = calUtil.getPreviousMonthFirst();
			endDate = calUtil.getPreviousMonthEnd();
		}else if("year".equals(dateType)){
			date = calUtil.getPreviousYear();
			 calUtil = new CalendarUtil();
			startDate  = calUtil.getPreviousYearFirst();
			endDate = calUtil.getPreviousYearEnd();
		}
		List<ReportDataVO> list =findDatas(startDate,endDate);
		saveDates(list,type,date);
		
		
	}
	public List findDatas(String startDate,String endDate) {
		
		
		StringBuilder sql = new StringBuilder("");
		sql.append("select ca.ProvinceName as ments, pa.AREA_NAME as titles, count(*) as counts ");
		sql.append(" from LPR_RECORD lpr,CAR_AREA ca,QUERY_DMB qd,PARK_DMB pd,PARK_AREA pa");
		sql.append(" where 1=1");
		sql.append(" and pd.TYPE='1'");
		sql.append(" and substr(lpr.PLATENO,1,2) =ca.carNum");
		sql.append(" and lpr.NID = qd.NID ");
		sql.append(" and qd.NID = pd.NID");
		sql.append(" and pd.PARK_AREA_ID=pa.ID");
		
		/*if(!"合计".equals(ments)){
			sql.append(" and ca.CityName='"+ments+"'");
		}
		if(!"小计".equals(titles)){
			sql.append(" and pa.AREA_NAME='"+titles+"'");
		}*/
		if(!StringUtil.isBlank(startDate)){
			startDate = startDate+" 00:00:00";
			sql.append(" and lpr.RQSJ>='"+startDate+"'");
		}
		if(!StringUtil.isBlank(endDate)){
			endDate = endDate + " 23:59:59";
			sql.append(" and lpr.RQSJ<='"+endDate+"'");
		}
		sql.append(" group by ca.ProvinceName, pa.AREA_NAME");
		
		log.info("sql=========="+sql);
		List<ReportCountDataVO> list = DB4ObjUtil.execute4Obj(new ReportCountDataVO(), sql.toString());
		return list;
		
	}
	/*public List<ReportCountDataVO> findtotal(HttpServletRequest req) throws ServiceException {
		StringBuilder sql = new StringBuilder("");
		sql.append("select '合计' as ments, pa.AREA_NAME as titles, count(*) as counts ");
		sql.append(" from LPR_RECORD lpr,CAR_AREA ca,QUERY_DMB qd,PARK_DMB pd,PARK_AREA pa");
		sql.append(" where 1=1");
		sql.append(" and pd.TYPE='1'");
		sql.append(" and substr(lpr.PLATENO,1,2) =ca.carNum");
		
		sql.append(" and lpr.NID = qd.NID ");
		sql.append(" and qd.NID = pd.NID");
		sql.append(" and pd.PARK_AREA_ID=pa.ID");
	
		if(!"合计".equals(ments)){
			sql.append(" and ca.CityName='"+ments+"'");
		}
		if(!"小计".equals(titles)){
			sql.append(" and pa.AREA_NAME='"+titles+"'");
		}
		if(!StringUtil.isBlank(startDate)){
			sql.append(" and lpr.RQSJ>='"+startDate+"'");
		}
		if(!StringUtil.isBlank(endDate)){
			sql.append(" and lpr.RQSJ<='"+endDate+"'");
		}
		sql.append(" group by pa.AREA_NAME");
		
		log.info("sql=========="+sql);
		List<ReportCountDataVO> list = DB4ObjUtil.execute4Obj(new ReportCountDataVO(), sql.toString());
		
		
		return list;
	}
	public List<ReportCountDataVO> findsubtotal(HttpServletRequest req) throws ServiceException {
		StringBuilder sql = new StringBuilder("");
		sql.append("select ca.ProvinceName as ments, '小计' as titles, count(*) as counts ");
		sql.append(" from LPR_RECORD lpr,CAR_AREA ca,QUERY_DMB qd,PARK_DMB pd,PARK_AREA pa");
		sql.append(" where 1=1");
		sql.append(" and pd.TYPE='1'");
		sql.append(" and substr(lpr.PLATENO,1,2) =ca.carNum");
		sql.append(" and lpr.NID = qd.NID ");
		sql.append(" and qd.NID = pd.NID");
		sql.append(" and pd.PARK_AREA_ID=pa.ID");
		if(!"合计".equals(ments)){
			sql.append(" and ca.CityName='"+ments+"'");
		}
		if(!"小计".equals(titles)){
			sql.append(" and pa.AREA_NAME='"+titles+"'");
		}
		if(!StringUtil.isBlank(startDate)){
			sql.append(" and lpr.RQSJ>='"+startDate+"'");
		}
		if(!StringUtil.isBlank(endDate)){
			sql.append(" and lpr.RQSJ<='"+endDate+"'");
		}
		sql.append(" group by ca.ProvinceName");
		
		log.info("sql=========="+sql);
		List<ReportCountDataVO> list = DB4ObjUtil.execute4Obj(new ReportCountDataVO(), sql.toString());
		
		
		return list;
	}
	public List<ReportCountDataVO> findalltotal(HttpServletRequest req) throws ServiceException {
		StringBuilder sql = new StringBuilder("");
		sql.append("select '合计' as ments, '小计' as titles, count(*) as counts ");
		sql.append(" from LPR_RECORD lpr,CAR_AREA ca,QUERY_DMB qd,PARK_DMB pd,PARK_AREA pa");
		sql.append(" where 1=1");
		sql.append(" and pd.TYPE='1'");
		sql.append(" and substr(lpr.PLATENO,1,2) =ca.carNum");
		sql.append(" and lpr.NID = qd.NID ");
		sql.append(" and qd.NID = pd.NID");
		if(!"合计".equals(ments)){
			sql.append(" and ca.CityName='"+ments+"'");
		}
		if(!"小计".equals(titles)){
			sql.append(" and pa.AREA_NAME='"+titles+"'");
		}
		sql.append(" and pd.PARK_AREA_ID=pa.ID");
		if(!StringUtil.isBlank(startDate)){
			sql.append(" and lpr.RQSJ>='"+startDate+"'");
		}
		if(!StringUtil.isBlank(endDate)){
			sql.append(" and lpr.RQSJ<='"+endDate+"'");
		}
		log.info("sql=========="+sql);
		List<ReportCountDataVO> list = DB4ObjUtil.execute4Obj(new ReportCountDataVO(), sql.toString());
		
		
		return list;
	}*/
	
	@Override
	public List<ReportTitleVO> findTitles(HttpServletRequest req) throws ServiceException {
		StringBuilder sql = new StringBuilder("");
		sql.append("select AREA_NAME as code,AREA_NAME as name from park_area where PID<>'0'");
		List<ReportTitleVO> list = DB4ObjUtil.execute4Obj(new ReportTitleVO(), sql.toString());
		list.add(new ReportTitleVO("小计","小计"));
		return list;
	}
	
	@Override
	public List<ReportTitleVO> findMents(HttpServletRequest req) throws ServiceException {
		StringBuilder sql = new StringBuilder("");
		sql.append("select ProvinceName as code,ProvinceName as name from CAR_AREA where 1=1 and CityName='"+ments+"'");
		List<ReportTitleVO> list = DB4ObjUtil.execute4Obj(new ReportTitleVO(), sql.toString());
		
		for (int i = 0; i < list.size(); i++) {
			ReportTitleVO v = list.get(i);
			String u = String.valueOf(mentMap.get("小计_"+v.getCode()));
			v.setUnit(u);
		}
		Collections.sort(list,new Comparator<ReportTitleVO>(){
			public int compare(ReportTitleVO obj1,ReportTitleVO obj2){
				if(StringUtil.isBlank(obj1.getUnit())&&StringUtil.isBlank(obj2.getUnit())){
					return 0;
				}
				//return order1-order2;
				if(StringUtil.isBlank(obj1.getUnit())){
					return 1;
				}
				
				if(StringUtil.isBlank(obj2.getUnit())){
					return -1;
				}
				int order1 = Integer.parseInt(obj1.getUnit());
				int order2 = Integer.parseInt(obj2.getUnit());
				return order2-order1;
			}
		});
		list.add(new ReportTitleVO("合   计","合计"));
		return list;
	}
	
	
	
	public String getMents() {
		return ments;
	}
	public void setMents(String ments) {
		this.ments = ments;
	}
	public String getTitles() {
		return titles;
	}
	public void setTitles(String titles) {
		this.titles = titles;
	}
}
